package com.gitee.springboot_userdemo.controller;

import com.gitee.springboot_userdemo.domain.User;
import com.gitee.springboot_userdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //显示用户列表
    @GetMapping("/list")
    public String list(Model model) {
        List<User> users = userService.selectAll();
        model.addAttribute("users", users);
        return "user-list";
    }

    //根据id删除用户
    @GetMapping("/delectById")
    public String delectById(int uid) {
        userService.deleteById(uid);

        return "redirect:/user/list";
    }

    //跳转至更新页面
    @GetMapping("/updateUI")
    public String updateUI(int uid, Model model) {
        User user = userService.selectByUid(uid);
        model.addAttribute("user", user);
        return "user-update";
    }
    //更新用户数据
    @PostMapping("/update")
    public String update(User user){
        System.out.println("修改："+user.getUid());
        userService.updateUser(user);
        return "redirect:/user/list";
    }

    //跳转添加页面
    @GetMapping("/addUI")
    public String addUI(){
        return "user-add";
    }
    //添加用户
    @PostMapping("/addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/user/list";
    }
}
